package bigdata.monitor.binlog.test;

import bigdata.monitor.binlog.entity.MonitorResult;
import bigdata.monitor.binlog.entity.MonitorRule;
import bigdata.monitor.binlog.mapper.MonitorRuleMapper;
import bigdata.monitor.binlog.service.RuleService;
import bigdata.monitor.binlog.utils.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.UUID;

@SpringBootTest
public class Test01 {

    @Autowired
    private MonitorRuleMapper monitorRuleMapper;

    @Autowired
    private RuleEvaluator ruleEvaluator;

    @Autowired
    private RuleService ruleService;

    @Test
    public void test01() {
        MonitorRule monitorRule = monitorRuleMapper.selectById(1);
        System.out.println(monitorRule);
    }

    @Test
    public void test02() {
        MonitorRule rule01 = monitorRuleMapper.selectById(1);
        String s1 = RuleConditionConverter.toDorisExpression(rule01);
        System.out.println(s1);
    }

    @Test
    public void test03() {
        List<MonitorRule> rules = monitorRuleMapper.selectList(null);
//        System.out.println(rules);
        String cdcJson = "{ \"table\": \"linkman_information\", \"op\": \"u\", \"before\": { \"id\": \"1001\", \"contact_info\": \"12345678\", \"delete_flag\": 0, \"phone\": \"13812345678\" }, \"after\": { \"id\": \"1001\", \"contact_info\": \"0632-12345678\", \"delete_flag\": \"0\", \"phone\": \"1381234567\" } }";

        String res = CombinedRuleSqlBuilder.buildSql(cdcJson, rules);
        System.out.println(res);
    }

    @Test
    public void test04() {
        // 1. 一次性执行所有规则
        List<MonitorRule> rules = monitorRuleMapper.selectList(null);
        String binlogJsonMessage = "{ \"table\": \"linkman_information\", \"op\": \"u\", \"before\": { \"id\": \"1001\", \"contact_info\": \"12345678\", \"delete_flag\": 0, \"phone\": \"13812345678\" }, \"after\": { \"id\": \"1001\", \"contact_info\": \"0632-12345678\", \"delete_flag\": \"0\", \"phone\": \"1381234567\" } }";
        List<MonitorRule> failedRules = ruleEvaluator.evaluateRules(binlogJsonMessage, rules);
        // System.out.println(failedRules);
        String tableName = "linkman_information";

        // 2. 保存失败结果
        for (MonitorRule rule : failedRules) {
            String changedFields = JsonPathFieldExtractor.extractChangedFields(binlogJsonMessage, rule.getOutputFields());
            MonitorResult result = new MonitorResult();
            result.setTableName(tableName);
            result.setPrimaryKey(JsonUtils.getPrimaryKey(binlogJsonMessage, rule.getPrimaryField()));
            result.setBatchId(UUID.randomUUID().toString());
            result.setRuleName(rule.getRuleName());
            result.setRuleCode(rule.getRuleCode());
            result.setChangedFields(changedFields);
            System.out.println(result);
        }
    }

    @Test
    public void test05() {
//        ruleService.refreshRules();
        List<MonitorRule> rules = ruleService.getSortedRulesByKey("customer_service.lake_company_linkman_information");
        System.out.println(rules);
    }

}
